let http = require("http");
let fs = require("fs");
let url = require("url");

let num = 0;
// 创建一个http服务
let server = http.createServer((request,response) => {
    let urlObj = url.parse(request.url,true)
    response.setHeader("content-type","text/html;charset=utf-8");
    console.log(urlObj);
    if(urlObj.pathname == "/"){
        let read = fs.readFileSync("./服务器.html");
        response.end( read.toString("utf-8") );
        return;
    }

    if(urlObj.pathname == "/game"){
        response.end("<h1>游戏频道</h1>");
        return;
    }

    if(urlObj.pathname == "/index"){
        response.end("<h1>这是首页</h1>");
        return;
    }
    if(urlObj.pathname == "/index.css"){
        response.setHeader("content-type","text/css;charset=utf-8");
        let read = fs.readFileSync("./index.css");
        response.end(read.toString("utf-8"));
        return;
    }
    
    if(urlObj.pathname == "/1.jpg"){
        response.setHeader("content-type","image/jpeg");
        let read = fs.readFileSync("./1.jpg");
        response.end(read);
        return;
    }
    if(urlObj.pathname == "/form"){
        let {query:{username,sex}} = urlObj;
        response.end(`<h1>${username}提交成功</h1>`)
        return;
    }


    response.end("<h1>对不起，找不到您需要的网页</h1>");

});

// 启动http服务监听
server.listen(3008,() => {
    console.log("服务器正在运行");
});